//
//  RegisterViewController.m
//  TestARC
//
//  Created by 晓泉 吴 on 12-9-22.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "RegisterViewController.h"
#import "JSON/SBJson.h"
#import "MyExtensions.h"

@interface RegisterViewController ()

@end

@implementation RegisterViewController

@synthesize emailTF,userNameTF,passwordTF1,passwordTF2,agreeSwitch,changeControl;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title=@"注册";
    
    
    [self.passwordTF1 setSecureTextEntry:YES];
    [self.passwordTF2 setSecureTextEntry:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.emailTF = nil;
    self.userNameTF = nil;
    self.passwordTF1 = nil;
    self.passwordTF2 = nil;
    request = nil;
    self.agreeSwitch = nil;
    self.changeControl = nil;
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)registetToYaoXia:(id)sender
{
    NSString *emailaddr = [self.emailTF text];
    NSString *disname = [self.userNameTF text];
    NSString *password1 = [self.passwordTF1 text];
    NSString *password2 = [self.passwordTF2 text];
    
    if ([emailaddr isEmpty]||
        [disname isEmpty]||
        [password1 isEmpty]||
        [password2 isEmpty]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"注意"
                                                       message:@"请输入完整的注册信息"
                                                      delegate:self
                                             cancelButtonTitle:@"确定"
                                             otherButtonTitles:nil];
        [alert show];
        return;
    }
    if (! [password1 isEqualToString:password2]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"注意"
                                                       message:@"两次输入的密码需要保持一致"
                                                      delegate:self
                                             cancelButtonTitle:@"确定"
                                             otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    if (self.changeControl.selectedSegmentIndex == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"注意"
                                                       message:@"必须同意药侠网使用条款才可以注册"
                                                      delegate:self
                                             cancelButtonTitle:@"确定"
                                             otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    [self showWaitView:nil];
    
    int timestamp = (int)[[NSDate date]timeIntervalSince1970];
    NSString * checksum = [[NSString stringWithFormat:@"%@%d%@%d%@yaoxia",emailaddr,timestamp,password1,timestamp,disname]md5];
    
    NSString * URL_REGISTER = [NSString stringWithFormat:@"http://www.yaoxia.com/ajaxServlet?style=b20d020a02da7ab3feb835e0a6ef0dcd&emailaddr=%@&password1=%@&password2=%@&disname=%@&timestamp=%d&checksum=%@",emailaddr,password1,password2,disname,timestamp,checksum];
    
    NSURL *url = [NSURL URLWithString:URL_REGISTER];
    request = [ASIHTTPRequest requestWithURL:url];
    [request setCompletionBlock:^{
        
        [self hideWaitView];
        // Use when fetching text data
        NSString *responseString = [request responseString];
        
        //NSLog(@"responseString = %@",responseString);
        id json = [responseString JSONValue];
        //NSLog(@"meg = %@",[json objectForKey:@"drugInfo"]);
        if ([[json objectForKey:@"code"]intValue] == 500) {
            NSLog(@"%@",[json objectForKey:@"message"]);
            
        }
        
        UIAlertView * messageAlert = [[UIAlertView alloc]
                                      initWithTitle:@"注册信息"
                                      message:[NSString stringWithFormat:@"%@",[json objectForKey:@"message"]]
                                      delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
        [messageAlert show];
        // Use when fetching binary data
        //NSData *responseData = [request responseData];
    }];
    [request setFailedBlock:^{
        [self hideWaitView];
        NSError *error = [request error];
        
        NSLog(@"error = %@",error);
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"无法连接到网络"
                              message:nil
                              delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }];
    [request startAsynchronous];
    
}
- (IBAction)backgroundTap:(id)sender
{
    [self.emailTF resignFirstResponder];
    [self.userNameTF resignFirstResponder];
    [self.passwordTF1 resignFirstResponder];
    [self.passwordTF2 resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self backgroundTap:nil];
    return TRUE;
}

@end
